package com.xuelang.pipeline.artifact.infrastructure.resource.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.artifact.client.common.constant.Constant;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.resource.dto.ArtifactProxyQuery;
import com.xuelang.pipeline.artifact.domain.resource.ArtifactProxy;
import com.xuelang.pipeline.artifact.domain.resource.repository.ArtifactProxyRepository;
import com.xuelang.pipeline.artifact.infrastructure.resource.converter.ArtifactProxyToArtifactProxyDOConverter;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactAttributeDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactProxyDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.mapper.ArtifactProxyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 制品仓库代理实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class ArtifactProxyRepositoryImpl implements ArtifactProxyRepository {

    @Autowired
    ArtifactProxyMapper artifactProxyMapper;

    @Override
    public PageRecord<ArtifactProxy> pageList(long currentPage, long pageSize, ArtifactProxyQuery artifactProxyQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<ArtifactProxy> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<ArtifactProxyDO> wrapper = new MPJLambdaWrapper<ArtifactProxyDO>();
        wrapper.selectAll(ArtifactProxyDO.class)
                .eq(ArtifactProxyDO::getGroupArtifactUuid, StrUtil.isNotBlank(artifactProxyQuery.getGroupArtifactUuid()) ? artifactProxyQuery.getGroupArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.eq(StrUtil.isNotBlank(artifactProxyQuery.getProxyArtifactUuid()), ArtifactProxyDO::getProxyArtifactUuid, artifactProxyQuery.getProxyArtifactUuid())
                .eq(StrUtil.isNotBlank(artifactProxyQuery.getArtifactType()), ArtifactProxyDO::getArtifactType, artifactProxyQuery.getArtifactType())
                .apply(StrUtil.isNotBlank(artifactProxyQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactProxyQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactProxyQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactProxyQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        artifactProxyMapper.selectJoinPage(page, ArtifactProxy.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords()
        );
    }

    @Override
    public List<ArtifactProxy> list(ArtifactProxyQuery artifactProxyQuery) {
        MPJLambdaWrapper<ArtifactProxyDO> wrapper = new MPJLambdaWrapper<ArtifactProxyDO>();
        wrapper.selectAll(ArtifactProxyDO.class)
                .eq(ArtifactProxyDO::getGroupArtifactUuid, StrUtil.isNotBlank(artifactProxyQuery.getGroupArtifactUuid()) ? artifactProxyQuery.getGroupArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.eq(StrUtil.isNotBlank(artifactProxyQuery.getProxyArtifactUuid()), ArtifactProxyDO::getProxyArtifactUuid, artifactProxyQuery.getProxyArtifactUuid())
                .eq(StrUtil.isNotBlank(artifactProxyQuery.getArtifactType()), ArtifactProxyDO::getArtifactType, artifactProxyQuery.getArtifactType())
                .apply(StrUtil.isNotBlank(artifactProxyQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactProxyQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactProxyQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactProxyQuery.getEndDate())
                .orderByDesc(ArtifactAttributeDO::getCreateTime);
        return artifactProxyMapper.selectJoinList(ArtifactProxy.class, wrapper);
    }


    @Override
    public Boolean save(ArtifactProxy artifactProxy) {
        return artifactProxyMapper.insert(ArtifactProxyToArtifactProxyDOConverter.INSTANCE.artifactProxyToArtifactProxyDO(artifactProxy)) == 1;
    }

    @Override
    public Boolean update(ArtifactProxy artifactProxy) {
        return artifactProxyMapper.update(
                ArtifactProxyToArtifactProxyDOConverter.INSTANCE.artifactProxyToArtifactProxyDO(artifactProxy),
                Wrappers.lambdaQuery(ArtifactProxyDO.class).eq(ArtifactProxyDO::getProxyArtifactUuid, artifactProxy.getProxyArtifactUuid())) == 1;
    }

    @Override
    public Boolean delete(String artifactProxyUuid) {
        return artifactProxyMapper.delete(Wrappers.lambdaQuery(ArtifactProxyDO.class)
                .eq(ArtifactProxyDO::getProxyArtifactUuid, artifactProxyUuid)) == 1;
    }

    @Override
    public ArtifactProxy query(String artifactProxyUuid) {
        ArtifactProxyDO artifactProxyDO = artifactProxyMapper.selectOne(Wrappers.lambdaQuery(ArtifactProxyDO.class).eq(ArtifactProxyDO::getProxyArtifactUuid, artifactProxyUuid));
        return ArtifactProxyToArtifactProxyDOConverter.INSTANCE.artifactProxyDOToArtifactProxy(artifactProxyDO);
    }

    @Override
    public Boolean deleteByGroupArtifactUuid(String artifactUuid) {
        return artifactProxyMapper.delete(Wrappers.lambdaQuery(ArtifactProxyDO.class)
                .eq(ArtifactProxyDO::getGroupArtifactUuid, artifactUuid)) > 0;
    }

    @Override
    public Boolean existsCurrentProxy(String groupArtifactUuid, String sourceArtifactUuid) {
        return artifactProxyMapper.exists(Wrappers.lambdaQuery(ArtifactProxyDO.class)
                .eq(ArtifactProxyDO::getGroupArtifactUuid, groupArtifactUuid)
                .eq(ArtifactProxyDO::getSourceArtifactUuid, sourceArtifactUuid)
        );
    }

}
